//
//  WXComposeToolBar.swift
//  weibo
//
//  Created by 王旭 on 16/4/26.
//  Copyright © 2016年 王旭. All rights reserved.


// 写微博控制器底部的ToolBar

import UIKit

//ToolBar按钮类型枚举
enum ToolBarButtonType : Int {
    case Picture = 0
    case  Mention
    case  Trend
    case  Emoticon
    case  Add
}


class WXComposeToolBar: UIStackView {
    
    //定义一个判定是否为系统键盘的属性
    var isSystemKeyBoard : Bool = true {
        didSet{
            var imageNamed = "compose_emoticonbutton_background"
            
            if isSystemKeyBoard == false {
                imageNamed = "compose_keyboardbutton_background"
            }
            //获取到按钮
            let button = viewWithTag((ToolBarButtonType.Emoticon).rawValue) as! UIButton
            //设置按钮的图片
            button.setImage(UIImage(named: imageNamed), forState: .Normal)
            button.setImage(UIImage(named: "\(imageNamed)_highligted"), forState: .Highlighted)
        }
    }
    
    //定义ToolBar按钮点击的闭包
    var toolBarButtonClickClosure : ((type:ToolBarButtonType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        //设置子控件填充方式
        distribution = .FillEqually
        //添加自按钮
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        addChildButton("compose_add_background", type: .Add)
        
    }
    
//设置子添加子按钮的方法
    private func addChildButton(imageNamed:String,type:ToolBarButtonType) {
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: "toolBarButtonClick:", forControlEvents: .TouchUpInside)
        //给button设置tag
        button.tag = type.rawValue
        //设置图片
        button.setImage(UIImage(named: "\(imageNamed)"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageNamed)_highlighted"), forState: UIControlState.Highlighted)
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        //添加到stackView上
        addArrangedSubview(button)
    }
    
//子按钮点方法
    @objc private func toolBarButtonClick(button:UIButton){
        toolBarButtonClickClosure?(type:ToolBarButtonType(rawValue: button.tag)!)
    }
    
}
